package com.fxg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxg.entity.Goods;
import com.fxg.mapper.GoodsMapper;
import com.fxg.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void buyGoods() {

        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("id",1001);

        if (goodsMapper.selectOne(wrapper).getNumber() > 0) {
            synchronized (GoodsService.class) {
                Goods goods = goodsMapper.selectOne(wrapper);
                if (goods.getNumber() > 0) {
                    Goods g = new Goods();
                    g.setNumber(goods.getNumber()-1);
                    goodsMapper.update(g,wrapper);
                    System.out.println("现在的库存为="+goodsMapper.selectOne(wrapper).getNumber());
                    return;
                }
            }

        }

        System.out.println("卖完了！");
    }
}
